import 'dart:io';

import 'package:base_moudle/index.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

import '../../../../new_paltform/define/hy_header.dart';
import '../../../../new_paltform/utils/json_parse_utils.dart';
import '../hy_work_home_logic.dart';
import 'hy_wrap_item_cell.dart';

class ProjectProgressCell extends StatelessWidget {
  HyWorkHomeLogic logic;

  ProjectProgressCell(this.logic);

  @override
  Widget build(BuildContext context) {
    return Obx(() => zs_round_radius_contianer(
        margin: const EdgeInsets.only(
          top: 6,
        ),
        padding: const EdgeInsets.only(left: 18, right: 18),
        allRadius: 6,
        color: ColorHex('#FFFFFF'),
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                ZsText(
                  '当前项目进度',
                  fontWeight: FontWeight.bold,
                  fontSize: 16,
                ),
                Row(
                  children: [
                    ZsText(
                      '任务进度',
                      color: zs_mainColor,
                    ),
                    Icon(
                      Icons.arrow_forward_ios_rounded,
                      size: 16,
                      color: zs_color_ccc,
                    )
                  ],
                ).intoGestureDetector(onTap: () {
                  if (logic.hasSchedule) {
                    logic.intentTaskSchedule();
                  } else {
                    EasyLoading.showInfo('该项目暂无进度');
                  }
                })
              ],
            ).intoContainer(height: 48),
            Column(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Column(
                      children: [
                        ZsText(
                          '总工期',
                          color: ColorHex('#FF7D22', opcity: 0.5),
                          fontWeight: FontWeight.w600,
                        ),
                        ZsText(
                          '${logic.totalDays}',
                          color: zs_mainColor,
                          fontSize: 32,
                          fontFamily: 'din',
                          top: 10,
                        ),
                      ],
                    ),
                    ZsCircleProgressView(
                      progress: logic.totalSchedule.value,
                      children: [
                        ZsText(
                          '总项目进度',
                          color: zs_black_999,
                        ),
                        ZsText(
                          logic.totalScheduler.value,
                          color: zs_black_333,
                          fontSize: 36,
                          fontFamily: 'din',
                        ),
                        ZsText(
                          logic.overdue.value ? '已超期' : '未超期',
                          color: zs_mainColor,
                          fontWeight: FontWeight.w600,
                        ),
                      ],
                    ).intoFlexible(flex: 3),
                    Column(
                      children: [
                        ZsText(
                          '目前进行',
                          color: ColorHex('#FF7D22', opcity: 0.5),
                          fontWeight: FontWeight.w600,
                        ),
                        ZsText(
                          '${logic.finishDays}',
                          color: zs_mainColor,
                          fontSize: 32,
                          fontFamily: 'din',
                          top: 10,
                        ),
                      ],
                    ),
                  ],
                ),
                GridView.builder(
                  gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 2,
                      crossAxisSpacing: 15,
                      childAspectRatio: 1.35,
                      mainAxisSpacing: 1),
                  physics: const NeverScrollableScrollPhysics(),
                  //增加
                  shrinkWrap: true,
                  //增加
                  itemCount: 2,
                  itemBuilder: (BuildContext ctx, int index) {
                    return zs_round_radius_contianer(
                            allRadius: 6,
                            margin: const EdgeInsets.only(top: 40, bottom: 10),
                            color: ColorHex('#F9F9F9'),
                            gradient:
                                LinearGradient(
                                    begin: Alignment.topCenter,
                                    end: Alignment.bottomCenter,
                                    colors:
                                        index ==
                                                0
                                            ? [
                                                ColorHex('#ECF2FF'),
                                                ColorHex('#F6F9FF')
                                              ]
                                            : [
                                                ColorHex('#E3F5FF'),
                                                ColorHex('#ECFAFF')
                                              ]),
                            child: Row(
                              children: [
                                if (index == 0)
                                  Icon(
                                    Iconfont.record_icon,
                                    size: 20,
                                    color: zs_black_333,
                                  ).marginOnly(left: 10)
                                else
                                  Icon(
                                    Iconfont.gante_charts,
                                    size: 20,
                                    color: zs_black_333,
                                  ).marginOnly(left: 10),
                                ZsText(
                                  index == 0 ? '历史记录' : '甘特图',
                                  left: 10,
                                  color: index == 0
                                      ? ColorHex('#02216B')
                                      : ColorHex('#094761'),
                                )
                              ],
                            ))
                        .intoSizedBox(height: 40)
                        .intoGestureDetector(onTap: () {
                      if (index == 0) {
                        if (logic.hasSchedule) {
                          _bottomSheet(context, logic);
                        } else {
                          EasyLoading.showInfo('该项目暂无进度');
                        }
                      }
                      if (index == 1) {
                        final Map<String, dynamic> json =
                            logic.projectDetail.value.toJson();
                        final Map<String, dynamic> map = <String, dynamic>{};
                        map.put('token',
                            ZsMemeryCache.loginDataModel!.tokens!.accessToken);
                        map.put('projectId', json['pid']);
                        map.put('projectName', json['projectName']);
                        if (Platform.isIOS) {
                          methodChannel.invokeMethod(
                              '甘特图', logic.projectDetail.value.toJson());
                        } else {
                          methodChannel.invokeMethod('甘特图', map);
                        }
                      }
                    });
                  },
                ),
                zs_round_radius_contianer(
                    allRadius: 6,
                    margin: const EdgeInsets.only(top: 2),
                    color: ColorHex('#F9F9F9'),
                    gradient: LinearGradient(
                        begin: Alignment.topCenter,
                        end: Alignment.bottomCenter,
                        colors: [ColorHex('#F0F2F7'), ColorHex('#FAFAFA')]),
                    child: GridView.builder(
                      gridDelegate:
                          const SliverGridDelegateWithFixedCrossAxisCount(
                              crossAxisCount: 3,
                              crossAxisSpacing: 1,
                              childAspectRatio: 1.2,
                              mainAxisSpacing: 1),
                      physics: const NeverScrollableScrollPhysics(),
                      //增加
                      shrinkWrap: true,
                      //增加
                      itemCount: logic.scheduleList.length,
                      itemBuilder: (BuildContext ctx, int index) {
                        return WrapItemCell(
                          content: '${logic.scheduleList[index].total}',
                          title: logic.scheduleList[index].type,
                        ).intoContainer();
                      },
                    )).intoContainer(),
              ],
            ).paddingOnly(top: 20, bottom: 10)
          ],
        )));
  }
}

///底部弹框
Future _bottomSheet(BuildContext context, HyWorkHomeLogic logic) {
  return showModalBottomSheet(
    context: context,
    isScrollControlled: true,
    backgroundColor: Colors.white,
    elevation: 10,
    shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
            topLeft: Radius.circular(30), topRight: Radius.circular(30))),
    builder: (BuildContext context) {
      return Obx(() => Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Container(
                    width: 20,
                  ),
                  ZsText('历史记录',
                      color: zs_black_333,
                      fontSize: 18,
                      fontWeight: FontWeight.w600),
                  const Icon(Icons.close).intoGestureDetector(onTap: () async {
                    Navigator.pop(context);
                  })
                ],
              ),
              ZsText('${logic.projectSchedule.value.projectName}',
                      color: zs_black_333, fontSize: 16)
                  .marginOnly(top: 18),
              ZsText('${logic.projectSchedule.value.projectCode}',
                      color: zs_black_999)
                  .marginOnly(bottom: 18),
              Container(color: zs_color_f2, width: Get.width, height: 1),
              Expanded(
                  child: ListView.builder(
                shrinkWrap: true,
                itemCount: logic.projectSchedule.value.recordList!.length,
                itemBuilder: (BuildContext ctx, int index) {
                  return Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Expanded(
                          child: Column(
                        children: [
                          Container(
                              color:
                                  index == 0 ? Colors.transparent : zs_color_d9,
                              width: 0.5,
                              height: 24),
                          Container(
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(6),
                                color: index == 0 ? zs_mainColor : zs_color_d9,
                              ),
                              width: 12,
                              height: 12),
                          Container(color: zs_color_d9, width: 0.5, height: 68),
                        ],
                      ).marginOnly(right: 12)),
                      Expanded(
                          flex: 9,
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Row(
                                children: [
                                  ClipOval(
                                    //圆形图片
                                    child: Image.asset(
                                      'assets/images/resImage/default_headerImage.png',
                                      width: 24,
                                      height: 24,
                                    ),
                                  ),
                                  ZsText('${logic.projectSchedule.value.recordList![index].updateUserName}',
                                          color: zs_black_333,
                                          fontWeight: FontWeight.w500)
                                      .marginOnly(left: 6, right: 10)
                                ],
                              ).marginOnly(top: 16, bottom: 6),
                              ZsText(
                                  '${logic.projectSchedule.value.recordList![index].proMessage}',
                                  color: zs_black_333,
                                  fontSize: 12),
                              ZsText('${logic.projectSchedule.value.recordList![index].createTime}',
                                      color: zs_black_999, fontSize: 12)
                                  .marginOnly(top: 6)
                            ],
                          ))
                    ],
                  );
                },
              ))
            ],
          ).intoContainer(
              padding: const EdgeInsets.only(left: 18, right: 18, top: 18),
              constraints:
                  BoxConstraints(maxHeight: Get.height - Get.statusBarHeight)));
    },
  );
}
